﻿using BBCore.Core.Models.Admins;
using BBCore.Core.Models.BaseModels;
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System.Data;
using System.Diagnostics.CodeAnalysis;

namespace BBCore.Server.Components.Pages.Samples
{
    /// <summary>
    /// 多参SQL查询,多个页面导出时会弹出选择相应的页面
    /// </summary>
    public partial class Dynamic_Content
    {
        [Parameter]
        public int AuthId { get; set; }
        private List<ControlDetails> ControlList = new();
        [NotNull]
        private string[]? Cn_columns { get; set; }
        private MenuEntity Menuentity = new();
        private Dictionary<string, string> Values = new();
        protected override void OnInitialized()
        {


            Menuentity = IAdminService.menu_GetmenuIncludeMany(AuthId);
            Cn_columns = Menuentity.CN_COLUMNS!.Split(',');//用逗号分割字符串成字符串数组
            ControlList = Menuentity.Query_Info!;
            Values = ControlList.ToDictionary(c => c.Label, c => "");


        }

        void ValueChanged(ChangeEventArgs a, string label)
        {
            Values[label] = a!.Value!.ToString()!;
        }


        private DataTable Dt { get; set; } = new DataTable();
        private async Task OnClickAsync()
        {
            //await Task.Delay(10000);//测试按钮是否出现等待的圈圈
            try
            {
                string sql = Menuentity.Query_Sql!;//取得sql语句
                foreach (var x in Values)//遍历Dictionary Values
                {
                    if (sql.Contains(x.Key)) //sql是否包含字典的键
                    {
                        sql = sql.Replace(x.Key, "'" + x.Value + "'");//替换
                    }
                }
                Dt = await ITempService.GetFqlToDtAsync(sql);
            }
            catch (Exception ex)
            {
                await SwalService.Show(new SwalOption()
                {
                    Category = SwalCategory.Error,
                    Title = "程序出错了!",
                    Content = "出错信息:" + ex.Message
                });
            }

        }
        [Inject]
        [NotNull]
        private IJSRuntime? JS { set; get; }
        private IJSObjectReference? Module;
        /// <summary>
        /// 导出数值型EXCEL
        /// </summary>
        private async void ExportToExcel()
        {
            try
            {
                await Module!.InvokeVoidAsync("exporttoxls");
            }
            catch (Exception ex)
            {
                nlogILogger?.LogError("多参SQL查询导出Excel出错:\r\n" + ex.ToString() + "\r\n");
            }

        }

        /// <summary>
        /// 导出文本型EXCEL
        /// </summary>
        private async void ExportToTextExcel()
        {
            try
            {
                await Module!.InvokeVoidAsync("exporttoTextxls");
            }
            catch (Exception ex)
            {
                nlogILogger?.LogError("多参SQL查询导出Excel出错:\r\n" + ex.ToString() + "\r\n");
            }

        }
        /// <summary>
        /// 加载自定义js文件
        /// </summary>
        /// <param name="firstRender"></param>
        /// <returns></returns>
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                await JS.InvokeAsync<IJSObjectReference>("import", "./JS/jquery.min.js");
                await JS.InvokeAsync<IJSObjectReference>("import", "./JS/jquery.tableToExcel.js");
                Module = await JS.InvokeAsync<IJSObjectReference>("import", "./JS/custom_js.js");
            }
        }


    }
}
